<?php
// +----------------------------------------------------------------------
// | Author: wyd 2025/10/12
// | DESC: 
// +----------------------------------------------------------------------

declare (strict_types=1);


/**********************************************************************************************************************/
#[Attribute(Attribute::TARGET_CLASS)]
class Animal
{
    public $name;
}

#[Animal(name:'123')]
class People
{

}

$reflectionClass = new ReflectionClass('People');
foreach ($reflectionClass->getAttributes() as $attribute){
    var_dump($attribute->getName());
    var_dump($attribute->getArguments());
}

die;
/**********************************************************************************************************************/

#[Attribute(Attribute::TARGET_CLASS)]
class Entity {
    public function __construct(
        public string $table
    ) {}
}

#[Attribute(Attribute::TARGET_PROPERTY)]
class Column {
    public function __construct(
        public string $type,
        public ?int $length = null
    ) {}
}

#[Entity(table: "users")]
class Users {
    #[Column(type: "string", length: 255)]
    private string $name;

    #[Column(type: "integer")]
    private int $age;
}

// 读取类注解
$reflectionClass = new ReflectionClass('Users');
$classAttributes = $reflectionClass->getAttributes(Entity::class);   //指定获取的属性，可以传空值获取所有;

foreach ($classAttributes as $attribute) {
    $entity = $attribute->newInstance();
    echo "Entity 表名: " . $entity->table . "\n";
}

// 读取属性注解
$reflectionProperties = $reflectionClass->getProperties();
foreach ($reflectionProperties as $property) {
    $propertyAttributes = $property->getAttributes(Column::class);
    foreach ($propertyAttributes as $attribute) {
        $column = $attribute->newInstance();
        echo "属性 {$property->getName()} 的列类型: {$column->type}, 长度: " . ($column->length ?? '无') . "\n";
    }
}

/*****************************************************************************************/